<?php 
namespace app\api\controller;
use think\Controller;
use app\common\lib\RedisServer;
use think\Db;
/**
 * 用户端公共控制器
 */
class Common extends Controller
{
	public $u_id='';
	public $u_type=1; //普通用户
	public function __construct()
	{
		parent::__construct();
		//获取token
		$token=getHeader('X-token');
		//dump($token);
		if(empty($token)){
			echo json_encode(array('code'=>501,'msg'=>'缺少登录参数'));
			exit();
		}
		//查询数据库中是否存在这个token
		$result=Db::name('admin_user')->where("token",$token)->find();
		if(!$result){
			//数据库中未查询到此token
			echo json_encode(array('code'=>502,'msg'=>'请重新登录'));
			exit;
		}
		//说明此用户存在且登录了
		if($result['status']==0){
			echo json_encode(array('code'=>202,'msg'=>'您的账号已经被冻结'));
			exit();
		}
		$this->u_id=$result['id'];//将用户的id返回
		//dump($this->u_id);
		//用户id加锁
		$check = RedisServer::lock($this->u_id);
        if($check == false){
            echo json_encode(array('code'=>202,'msg'=>'访问超时'));
            exit();
        }
	}


	protected function ajaxJson($code,$msg,$data=null){
	    @RedisServer::unlock($this->u_id);
		if(!isset($data)){
			return json(['code'=>$code,'msg'=>$msg]);
		}
	    return json(['code'=>$code,'msg'=>$msg,'data'=>$data]);
	}

	//获取自己的ixnx
    protected function get_me($uid,$field){
        return db('admin_user')->where(['id'=>$uid])->field($field)->find();
    }
}